package cn.cmcc.online.smsapi.sdk.service.pubInfo;

import cn.cmcc.online.smsapi.sdk.constant.Constant;
import cn.cmcc.online.smsapi.sdk.db.entity.pubinfo.PubInfoManager;
import cn.cmcc.online.smsapi.sdk.log.LogManager;
import cn.cmcc.online.smsapi.sdk.net.NetUtil;
/*SDK-616 hewengao 20160918 start*/
import android.os.Process;
/*SDK-616 hewengao 20160918 end*/

/* SDK-397 hewengao 20160527 start */
public class BackGroundRunnable implements Runnable {

	public final static int TYPE_UPDATE_TASK_1=1;
	public final static int TYPE_UPDATE_TASK_2=2;
	public String iccid =null;
	public String areaCode =null;
	public boolean isRun =false;
	private int type = 0;
	private BackGroundRunnable(){}
	public BackGroundRunnable(int type){
		this.type = type;
	}
	@Override
    public void run() {
        try {
            /* sdk-616 hewengao/20160918 start */
            android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
            /* sdk-616 hewengao/20160918 end */
            isRun = true;
            switch(type){
            case TYPE_UPDATE_TASK_1:
            	executeUpdateTask1();
            	break;
            case TYPE_UPDATE_TASK_2:
            	executeUpdateTask2();
            	break;
        	default:
        		break;
            }
		} catch (Throwable e) {
			LogManager.e(Constant.TAG, e.getMessage(), e);
		}finally{
            isRun =false;
            destory();
		}
	}

    private void executeUpdateTask2() {
        try {
            /* SDK-613 hewengao/20160908 start */
            if (NetUtil.checkAccessNetWork(2)) {
                PubInfoService.checkPubInfoUpdate(areaCode, iccid);
            }
            /* SDK-613 hewengao/20160908 end */
            PubInfoService.checkQueryMenuSql();// 检查更新
        } catch (Throwable e) {
            LogManager.e(Constant.TAG, e.getMessage(), e);
        }
	}
	private void executeUpdateTask1(){
		PubInfoService.checkQueryUnknowNum(areaCode, iccid, false);
		PubInfoService.checkQueryPubInfoNum(areaCode, iccid, false);
		/* SDK-496 zhaojiangwei 20160622 start */
		PubInfoManager.deleteRuleNumData();
		/* SDK-496 zhaojiangwei 20160622 end */
		synchronized (this) {
			PubInfoService.isExeUpdatePubInfo = false;
		}
	}
	public void destory(){
		this.iccid =null;
		this.areaCode =null;
	}
}
/* SDK-397 hewengao 20160527 end */
